package org.jeecg.modules.app.v1.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.modules.app.v1.shop.entity.*;
import org.jeecg.modules.app.v1.shop.service.ApiShopService;
import org.jeecg.modules.app.v1.util.Const;
import org.jeecg.modules.aspect.ApiLoginAspct;
import org.jeecg.modules.power.device.service.IYlDeviceService;
import org.jeecg.modules.power.group.entity.GroupUser;
import org.jeecg.modules.power.shop.entity.YlShop;
import org.jeecg.modules.power.shop.entity.YlShopCategory;
import org.jeecg.modules.power.shop.entity.YlTransferShopRecords;
import org.jeecg.modules.power.shop.service.IYlShopCategoryService;
import org.jeecg.modules.power.shop.service.IYlShopCollectService;
import org.jeecg.modules.power.shop.service.IYlShopService;
import org.jeecg.modules.power.shop.service.IYlTransferShopRecordsService;
import org.jeecg.modules.power.util.pack.Pack;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
* @Description: 门店表
* @Author: jeecg-boot
* @Date:   2024-07-08
* @Version: V1.0
*/
@Api(tags="APP管理端_门店")
@RestController
@RequestMapping("/api/v1/ylShop")
@Slf4j
@RequiredArgsConstructor
public class ApiShopController {

   private final IYlShopService ylShopService;
   private final IYlShopCategoryService shopCategoryService;
   private final ApiShopService apiShopService;
   private final IYlTransferShopRecordsService recordsService;
   private final IYlShopCollectService collectService;
    private final IYlDeviceService deviceService;

    /**
    *  添加
    * @param shopInfo
    * @return
    */
   @ApiLogin
   @ApiOperation(value="3_新增门店门店", notes="新增门店门店")
   @ApiOperationSupport(order = 3)
   @PostMapping(value = "/add")
   public Result<String> add(@RequestBody ApiAddShopEntity shopInfo, HttpServletRequest request) {
       SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
       GroupUser groupUser = deviceService.getGroupUser(bd.getId());
       shopInfo.setShopBd(groupUser.getUserInfo());
       shopInfo.setBdId(bd.getId());
       Pack<ApiAddShopEntity> pack = new Pack<>(shopInfo);
       if(pack.isSuccess)ylShopService.saveShopInfo(pack.getData(),bd);
       else return Result.error_599(pack.getMessage());
       return Result.OK("添加成功！");
   }

    /**
     *  修改
     * @param shopInfo
     * @return
     */
    @ApiLogin
    @ApiOperation(value="2_修改门店", notes="修改门店")
    @ApiOperationSupport(order = 2)
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody ApiAddShopEntity shopInfo) {
//        Pack<ApiAddShopEntity> pack = new Pack<>(shopInfo);
//        if(pack.isSuccess)
        ylShopService.editShopInfo(shopInfo);
//        else return Result.error_599(pack.getMessage());
        return Result.OK("添加成功！");
    }

    /**
     * 分页列表查询
     * *******************还需要增加后续统计数据*******************
     *
     * @return
     */
    @ApiLogin
    @ApiOperation(value="1_我的门店列表", notes="我的门店列表")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "门店状态1待审核2运营中3审核驳回4公海",name = "shopStatus")
    })
    @ApiOperationSupport(order = 1)
    @GetMapping(value = "/myShop")
    public Result<List<ApiListShopEntity>> myShop(String lng,String lat,String shopName,String shopStatus,
                                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                  HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        Page<ApiListShopEntity> page = new Page<>(pageNo,pageSize);
        IPage<ApiListShopEntity> shopIPage = ylShopService.mylist(page,  lat,  lng,  shopName,bd.getId(),shopStatus);
        return Result.OK(shopIPage.getRecords());
    }

    /**
     * 分页列表查询
     * @return
     */
    @ApiLogin
    @ApiOperation(value="4_门店详情", notes="门店详情")
    @ApiOperationSupport(order = 4)
    @GetMapping(value = "/myShopDetail")
    public Result<ApiShopEntity> myShopDetail(String id, HttpServletRequest request ) {
        ApiShopEntity shop = ylShopService.myShopDetail(id);
        return Result.OK(shop);
    }

    /**
     * 门店品类
     * @return
     */
    @ApiOperation(value="5_门店品类", notes="门店品类")
    @ApiOperationSupport(order = 5)
    @GetMapping(value = "/shopCategory")
    public Result<?> shopCategory() {
        List<ApiShopCategoryEntity> list = shopCategoryService.getList();
        return Result.OK(list);
    }

    /**
     * 门店BD列表
     * @return
     */
    @ApiOperation(value="6_门店BD列表", notes="门店BD列表")
    @ApiOperationSupport(order = 6)
    @GetMapping(value = "/bdList")
    public Result<?> shopBdList(String name) {
        List<ApiBdEntity> list = apiShopService.getBdList(name);
        return Result.OK(list);
    }

    /**
     *  门店转移提交
     * @param id 转移对象ID
     * @param shopId 要转移的门店ID
     * @param reason 转移的原因
     * @return
     */
    @ApiLogin
    @ApiOperation(value="7_门店转移提交", notes="门店转移提交")
    @ApiOperationSupport(order = 7)
    @PostMapping(value = "/transfer")
    public Result<String> transfer(String id,String shopId,String reason) {
        apiShopService.transfer(id, shopId, reason);
        return Result.OK("提交成功！");
    }

    /**
     *  门店转移确认
     * @param id 转移记录ID
     * @param flag 同意或拒绝标记  1同意2拒绝
     * @param reason 拒绝原因(拒绝时填写)
     * @return
     */
    @ApiLogin
    @ApiOperation(value="8_门店转移确认（弃用，改为后端审核）", notes="门店转移确认")
    @ApiOperationSupport(order = 8)
    @PostMapping(value = "/confirm")
    public Result<String> transferConfirm(String id,Integer flag,String reason) {
        apiShopService.confirm(id, flag, reason);
        return Result.OK("提交成功！");
    }

    /**
     * 门店转移记录
     * @return
     */
    @ApiLogin
    @ApiOperation(value="9_门店转移记录", notes="门店转移记录")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "门店ID",name = "shopId"),
            @ApiImplicitParam(value = "开始时间",name = "beginTime"),
            @ApiImplicitParam(value = "结束时间",name = "endTime")
    })
    @GetMapping(value = "/records")
    public Result<List<ApiTransferShopRecords>> transferRecords(String shopId,String beginTime,String endTime,
                                                                HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        List<YlTransferShopRecords> list = recordsService.getRecords(bd.getId(),shopId,beginTime,endTime);
        List<ApiTransferShopRecords> tlist = list.stream().map(records->{
            ApiTransferShopRecords records1 = new ApiTransferShopRecords();
            records1.setId(records.getId());
            records1.setTransferName(records.getTransferName());
            records1.setReceiveName(records.getReceiveName());
            records1.setTransferTime(records.getTransferTime());
            records1.setTransferType(Const.transferType_1);
            return records1;
        }).collect(Collectors.toList());
        return Result.OK(tlist);
    }

    /**
     * 门店转移记录
     * @return
     */
    @ApiLogin
    @ApiOperation(value="10_门店列表（其他人、公海）", notes="门店列表（其他人、公海）")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "状态-2：它人，4：公海",name = "status")
    })
    @ApiOperationSupport(order = 10)
    @GetMapping(value = "/shopList")
    public Result<List<ApiListShopEntity>> shopList(String lng,String lat,String status,String shopName,
                                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                    HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        Page<ApiListShopEntity> page = new Page<>(pageNo,pageSize);
        IPage<ApiListShopEntity> shopIPage = ylShopService.otherShopList(page,  lat,  lng,  status,shopName,bd.getId());
        return Result.OK(shopIPage.getRecords());
    }

    /**
     *  门店收藏
     * @param shopId 转移记录ID
     * @param flag 1收藏2取消
     * @return
     */
    @ApiLogin
    @ApiOperation(value="11_门店收藏/取消收藏（flag=1收藏2取消收藏）", notes="门店收藏/取消收藏")
    @ApiOperationSupport(order = 11)
    @PostMapping(value = "/collect")
    public Result<String> transferConfirm(Integer flag,String shopId,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        if(flag==1)collectService.collect(shopId, bd.getId() );
        if(flag==2)collectService.cancel(shopId, bd.getId() );
        return Result.OK(flag==1?"收藏成功！":"已取消");
    }

    /**
     * 收藏门店列表
     * @return
     */
    @ApiLogin
    @ApiOperation(value="12_收藏门店列表", notes="12_收藏门店列表")
    @ApiOperationSupport(order = 12)
    @GetMapping(value = "/collectList")
    public Result<List<ApiListShopEntity>> collectList(String lng,String lat,String shopName,
                                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                    HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        Page<ApiListShopEntity> page = new Page<>(pageNo,pageSize);
        IPage<ApiListShopEntity> shopIPage = ylShopService.collectList(page,  lat,  lng,shopName, bd.getId());
        return Result.OK(shopIPage.getRecords());
    }

    /**
     * 解绑到公海
     * @return
     */
    @ApiLogin
    @ApiOperation(value="13_解绑到公海", notes="解绑到公海")
    @ApiOperationSupport(order = 13)
    @GetMapping(value = "/sea")
    public Result<?> sea(String shopId,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        ylShopService.sea(shopId,bd);
        return Result.OK();
    }

    /**
     * 从公海认领门店
     * @return
     */
    @ApiLogin
    @ApiOperation(value="14_从公海认领门店", notes="从公海认领门店")
    @ApiOperationSupport(order = 14)
    @PostMapping(value = "/claim")
    public Result<?> claim(String shopId,HttpServletRequest request) {
        SysUser bd = (SysUser)request.getAttribute(ApiLoginAspct.SYS_USER);
        ylShopService.claim(shopId,bd);
        return Result.OK("认领成功");
    }

}
